import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:lucky_guy/widgets/form_text.dart';
import 'package:provider/provider.dart';
import 'package:lucky_guy/global.dart';
import 'package:flutter/material.dart';
import 'package:lucky_guy/main_manager.dart';

class EditPage extends StatefulWidget {
  EditPage({Key key}) : super(key: key);
  @override
  _EditPageState createState() => _EditPageState();
}

class _EditPageState extends State<EditPage> {
  @override
  void initState() {
    super.initState();
  }

  List<Widget> renderFormList(_mainManager) {
    List<Widget> list = [];
    for (int i = 0; i < _mainManager.allLuckyGuys.length; i++) {
      TextEditingController _formTextController = TextEditingController();
      _formTextController.text = _mainManager.allLuckyGuys[i];
      list.add(SizedBox(height: 10));
      list.add(Row(
        children: [
          Expanded(
            child: FormText(
              controller: _formTextController,
              hintText: '名称',
              onChanged: (text) {
                setState(() {
                  _mainManager.allLuckyGuys[i] = text;
                });
              },
            ),
          ),
          IconButton(
            icon: Icon(
              Icons.delete,
              color: Global.backgroundColor,
              size: 24,
            ),
            onPressed: () {
              setState(() {
                _mainManager.allLuckyGuys.removeAt(i);
              });
            },
          )
        ],
      ));
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {
    MainManager _mainManager = Provider.of<MainManager>(context);
    return Scaffold(
      body: FlutterEasyLoading(
        child: Container(
          padding: EdgeInsets.all(20),
          width: MediaQuery.of(context).size.width,
          height: MediaQuery.of(context).size.height,
          decoration: BoxDecoration(
            color: Global.backgroundColor,
            gradient: LinearGradient(
              begin: Alignment.topLeft,
              end: Alignment.bottomRight,
              colors: [
                Color(0xFF919191),
                Color(0xFFE8E7ED),
              ],
            ),
          ),
          child: Column(
            children: [
              SizedBox(height: Global.paddingTop),
              Row(
                children: [
                  InkWell(
                    onTap: () {
                      Navigator.of(context).pop();
                    },
                    child: Container(
                      child: Row(
                        children: [
                          Icon(
                            Icons.arrow_back_ios,
                            size: 15,
                            color: Global.backgroundColor,
                          ),
                          Text(
                            '返回',
                            style: TextStyle(
                              color: Global.backgroundColor,
                              fontSize: 14,
                              fontWeight: FontWeight.w600,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Expanded(
                    child: Center(
                      child: Text(
                        '编辑候选人',
                        style: TextStyle(
                          color: Global.backgroundColor,
                          fontSize: 16,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                    ),
                  ),
                  SizedBox(width: 43),
                ],
              ),
              SizedBox(height: 10),
              Expanded(
                child: SingleChildScrollView(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Column(
                        children: renderFormList(_mainManager),
                      ),
                      SizedBox(height: 30),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          InkWell(
                            onTap: () {
                              setState(() {
                                _mainManager.allLuckyGuys.add('');
                              });
                            },
                            child: Container(
                              padding: EdgeInsets.fromLTRB(30, 10, 30, 10),
                              decoration: BoxDecoration(
                                border: Border.all(
                                  color: Global.backgroundColor,
                                  width: 2,
                                ),
                                borderRadius: BorderRadius.circular(10),
                              ),
                              child: Row(
                                children: [
                                  Icon(
                                    Icons.add_circle,
                                    size: 20,
                                    color: Global.backgroundColor,
                                  ),
                                  SizedBox(width: 10),
                                  Text(
                                    '添加一个',
                                    style: TextStyle(
                                      color: Global.backgroundColor,
                                      fontSize: 15,
                                      fontWeight: FontWeight.w600,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                          InkWell(
                            onTap: () {
                              if (_mainManager.allLuckyGuys.length < 2) {
                                EasyLoading.showToast('请添加至少两个候选人');
                              } else if (_mainManager.allLuckyGuys
                                  .any((item) => item.trim() == '')) {
                                EasyLoading.showToast('请删除空白选项');
                              } else {
                                _mainManager.update(_mainManager.allLuckyGuys);
                                Navigator.of(context).pop();
                              }
                            },
                            child: Container(
                              padding: EdgeInsets.fromLTRB(45, 10, 45, 10),
                              decoration: BoxDecoration(
                                border: Border.all(
                                  color: Global.primaryColor,
                                  width: 2,
                                ),
                                color: Global.primaryColor,
                                borderRadius: BorderRadius.circular(10),
                              ),
                              child: Row(
                                children: [
                                  Icon(
                                    Icons.save,
                                    size: 20,
                                    color: Global.backgroundColor,
                                  ),
                                  SizedBox(width: 10),
                                  Text(
                                    '保存',
                                    style: TextStyle(
                                      color: Global.backgroundColor,
                                      fontSize: 15,
                                      fontWeight: FontWeight.w600,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          )
                        ],
                      )
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
